<template>
  <!-- 创建订单后的弹框 -->
  <div class="creation">
    <div class="header">
      <span class="order-title">发布公告</span>
      <div class="order-right">
        <span class="order-close">
          <i class="el-icon-close" @click="orderClose" />
        </span>
      </div>
    </div>
    <div class="footer">
      <div style="margin: 0 auto">
        <el-button type="primary">预览</el-button>
        <el-button type="primary" @click="onSubmit" id="orderSave"
          >保存</el-button
        >
        <el-button type="primary" @click="publish">发布</el-button>
        <el-button id="orderCancel" @click="orderClose">取消</el-button>
      </div>
    </div>
    <div class="article">
      <div class="noticeContainer">
        <el-form :rules="rules" ref="form" :model="form" label-width="80px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="可见范围" prop="region">
            <el-select
              v-model="form.region"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="必读公告" style="width: 300px">
            <el-switch v-model="form.delivery"></el-switch>
          </el-form-item>
          <el-form-item label="列表置顶">
            <el-switch v-model="form.delivery"></el-switch>
          </el-form-item> -->
          <el-form-item label="弹窗日期">
            <el-date-picker
              v-model="form.date1"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="弹窗时间">
            <el-time-picker
              v-model="form.date2"
              :picker-options="{
                selectableRange: '18:30:00 - 20:30:00',
              }"
              placeholder="任意时间点"
            >
            </el-time-picker>
          </el-form-item>
          <el-form-item label="label" class="ql-container">
            <div class="quill-wrap">
              <quill-editor v-model="form.content" :options="editorOption" />
            </div>
          </el-form-item>
          <el-form-item label="label">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-change="handleChange"
              :file-list="form.fileList"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div
                slot="tip"
                class="el-upload__tip"
                style="display: inline-block; margin-left: 20px; color: orange"
              >
                最多上传3个文件，每个文件不得超过2M :
              </div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import "quill/dist/quill.snow.css";
import { quillEditor, Quill } from "vue-quill-editor";
import { container, ImageExtend, QuillWatch } from "quill-image-extend-module";
Quill.register("modules/ImageExtend", ImageExtend);
import { reqAddNotice } from "@/api/message";
export default {
  components: { quillEditor },
  props: ["closeAdd", "displayNoticeTable", "noticeList"],
  data() {
    return {
      form: {
        title: "",
        region: "",
        date1: "",
        date2: new Date(2016, 9, 10, 18, 40),
        content: "",
        // delivery: false,
        fileList: [
          {
            name: "food.jpeg",
            url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          },
          {
            name: "food2.jpeg",
            url: "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100",
          },
        ],
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        region: [
          { required: true, message: "请输入可见范围", trigger: "blur" },
        ],
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      editorOption: {
        // 富文本框参数设置
        modules: {
          ImageExtend: {
            loading: true,
            name: "img",
            action: "http://127.0.0.1:7001/yun/upload",
            response: (res) => {
              return `http://127.0.0.1:7001/public/uploadfile/${res.data.filename}`;
            },
          },
          toolbar: {
            container: container,
            handlers: {
              image: function () {
                QuillWatch.emit(this.quill.id);
              },
            },
          },
        },
      },
      article: {
        checked: true,
      },
      masg: false,
      radio: "1",
      travelPeak: false,
      formData: {
        order_code: "",
        activity_id: 1,
        early_warning_id: 2,
        notice_id: 3,
      },
    };
  },
  computed: {
    // 计算属性的 getter
    alltotal_cost: function () {
      var totalMony =
        Number(this.formData.trans_cost) +
        Number(this.formData.collect_cost) +
        Number(this.formData.deliver_cost) +
        Number(this.formData.pack_cost) +
        Number(this.formData.handling_charge) +
        Number(this.formData.payment_collection) +
        Number(this.formData.insure_value) +
        Number(this.formData.insure_premium);
      return totalMony;
    },
  },
  methods: {
    handleChange() {
      //控制上传功能的
    },
    orderClose() {
      this.closeAdd();
      Object.assign(this.$data.form, this.$options.data().form);
    },
    // 保存按钮
    async onSubmit() {
      this.formData.order_code = this.form.title;
      const formData = new FormData();
      if (
        this.noticeList.find(
          (item) => item.order_code === this.formData.order_code
        )
      ) {
        this.$message({
          type: "error",
          message: `公告标题不允许重复`,
        });
      } else {
        formData.append("order_code", this.formData.order_code);
      }
      formData.append("activity_id", this.formData.activity_id);
      formData.append("early_warning_id", this.formData.early_warning_id);
      formData.append("notice_id", this.formData.notice_id);
      await reqAddNotice(formData);
      this.closeAdd();
      this.displayNoticeTable();
    },
    publish() {},
    dateFormat(dateData) {
      var date = new Date(dateData);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      const time = y + "-" + m + "-" + d;
      return time;
    },
  },
};
</script>

<style lang="less" scoped>
.creation {
  width: 700px;
  background: white;
  position: absolute;
  right: 0px;
  top: 0px;
  z-index: 100;
  // 头部信息
  .header {
    width: 100%;
    height: 36px;
    background: #5a6d82;
    line-height: 36px;
    .order-title {
      color: #fff;
      font-size: 14px;
      padding-left: 12px;
    }
    .order-right {
      position: absolute;
      right: 0;
      top: 0;
      height: 35px;
      line-height: 35px;
      .order-close {
        height: 35px;
        display: inline-block;
        overflow: hidden;
        position: relative;
        padding: 0 10px;
        cursor: pointer;
        color: #fff;
      }
    }
  }
  // 底部信息
  .footer {
    position: absolute;
    width: 100%;
    height: 20px;
    background: #f8f8f8;
    z-index: 100;
    bottom: 0px;
  }
  // 显示打印框的内容

  // 内容部分
  .article {
    width: 98%;
    height: calc(100vh - 128px);
    overflow: auto;
    margin: 10px auto;
    .noticeContainer {
      .ql-container {
        .quill-wrap {
          /deep/ .ql-editor p {
            min-height: 100px;
          }
        }
      }
    }
  }
}
</style>
